package com.hermes.action.json;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hermes.biz.common.model.comment.UserCommentDisplayUnitDO;
import com.hermes.biz.common.model.item.ItemDO;
import com.hermes.biz.common.model.result.UserCommentResultDO;
import com.hermes.biz.common.model.result.UserResultDO;
import com.hermes.biz.common.model.user.UserCommentDO;
import com.hermes.biz.common.util.string.StringUtil;
import com.hermes.biz.manager.user.UserCommentManager;
import com.hermes.biz.service.item.ItemService;
import com.hermes.biz.service.user.UserService;
import com.hermes.common.action.AsynBaseAction;
import com.hermes.common.util.ParameterParserUtil;

/**
 * 
 * Detail-Get-User-Comment
 * 
 */
public class AsynGetUserComments  extends AsynBaseAction{

	private static final long serialVersionUID = 2762245462524625871L;

	private UserCommentManager userCommentManager;
	
	private ItemService itemService;
	
	private UserService userService;
	
	private final String defaultPic = "2";
	
	private final String defaultName="quanquan";

	private Logger log = Logger.getLogger(AsynGetUserComments.class);

	private String constent ;

	public String itemPicUrl;
	
	public String itemTagId;
	
	public String itemIdNum;

	public List<UserCommentDisplayUnitDO> resultList ;

	@Override
	public String execute() {
		
		String itemIdStr = ParameterParserUtil.getParameter("itemId");
		itemIdNum=itemIdStr;
		log.error(itemIdStr);
		
		long itemId= 0 ;
		
		if(StringUtil.isNumOnly(itemIdStr)){
			itemId = Long.parseLong(itemIdStr);
		}
		
		log.error(itemId);
		
		ItemDO item = itemService.getSingleItemByItemId(itemId).getModule();
		if(item==null){
			setConstent("{}");
		}
		if(item!=null){
			itemPicUrl = item.getItemPicUrl();
			itemTagId = String.valueOf(item.getTagId());
			itemIdNum=String.valueOf(item.getItemId());
		}
		
		log.error("asy-get-home-comment-result:"+itemPicUrl+"|"+itemTagId);

		
		List<UserCommentDO> comment = userCommentManager.getUserCommentInfoByItemId(itemId);
		
		resultList = new ArrayList<UserCommentDisplayUnitDO>();
		
		for (UserCommentDO userCommentDO : comment){
				UserCommentDisplayUnitDO userCommentDisplayUnitDO = new UserCommentDisplayUnitDO();
				userCommentDisplayUnitDO.setComment(userCommentDO.getComment());
				userCommentDisplayUnitDO.setCreateTime(userCommentDO.getCreateTime());
				userCommentDisplayUnitDO.setUserId(userCommentDO.getCreateUserId());
				
				UserResultDO userDO = userService.getUserInfoByUserId(userCommentDO.getCreateUserId());
				if(userDO.isSuccess()){
					log.error("Data-of-user:"+userDO.getModule().getHeadPic()+"-----"+userDO.getModule().getUserName());
				}
				if(userCommentDO.getType()==2){
					userCommentDisplayUnitDO.setType(userCommentDO.getType());
					userCommentDisplayUnitDO.setToUserNick( userService.
							getUserInfoByUserId(userCommentDO.getOriginalUserId()).getModule().getUserName());
					UserCommentResultDO ucrDO = userCommentManager.getUserCommentInfoByCommentId(userCommentDO.getOriginalCommentId());
					if(ucrDO.isSuccess()){
						userCommentDisplayUnitDO.setOriginCommentContent(ucrDO.getModule().getComment());
						userCommentDisplayUnitDO.setOriginUserId(ucrDO.getModule().getCreateUserId());
					}
				}
				
				if(userDO.isSuccess() && userDO.getModule().getHeadPic()!=null){
					userCommentDisplayUnitDO.setHeadPic(userDO.getModule().getHeadPic());
					userCommentDisplayUnitDO.setUserName(userDO.getModule().getUserName());
				}
				else{
					userCommentDisplayUnitDO.setHeadPic(defaultPic);
					userCommentDisplayUnitDO.setUserName(defaultName);
				}
				resultList.add(userCommentDisplayUnitDO);
		}
		
		log.error("asy-get-home-comment-daxiao:"+resultList.size());
		
		return SUCCESS;
	}


	public void setUserCommentManager(UserCommentManager userCommentManager) {
		this.userCommentManager = userCommentManager;
	}
	
	public ItemService getItemService() {
		return itemService;
	}


	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}


	public String getConstent() {
		return constent;
	}


	public void setConstent(String constent) {
		this.constent = constent;
	}
	public List<UserCommentDisplayUnitDO> getResultList() {
		return resultList;
	}

	public void setResultList(List<UserCommentDisplayUnitDO> resultList) {
		this.resultList = resultList;
	}
	
	public UserService getUserService() {
		return userService;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public String getItemPicUrl() {
		return itemPicUrl;
	}


	public void setItemPicUrl(String itemPicUrl) {
		this.itemPicUrl = itemPicUrl;
	}

	public String getItemTagId() {
		return itemTagId;
	}


	public void setItemTagId(String itemTagId) {
		this.itemTagId = itemTagId;
	}


	public String getItemIdNum() {
		return itemIdNum;
	}


	public void setItemIdNum(String itemIdNum) {
		this.itemIdNum = itemIdNum;
	}


}
